//
//  ViewController.m
//  Tomcat
//
//  Created by qianfeng001 on 15/6/30.
//  Copyright (c) 2015年 li. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageCatDrink;
- (IBAction)drink:(id)sender;
- (IBAction)eat:(id)sender;
- (IBAction)fart:(id)sender;
- (IBAction)scratch:(id)sender;
- (IBAction)pie:(id)sender;
- (IBAction)cymbal:(id)sender;
- (IBAction)foot_right:(id)sender;
- (IBAction)foot_left:(id)sender;

- (IBAction)angry:(id)sender;
- (IBAction)stomach:(id)sender;

- (IBAction)fail:(id)sender;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)drink:(id)sender {
   
     [self startAnimating:81 pictureName:@"drink"];

}

- (IBAction)eat:(id)sender {
     [self startAnimating:40 pictureName:@"eat"];
}

- (IBAction)fart:(id)sender {
     [self startAnimating:28 pictureName:@"fart"];

}

- (IBAction)scratch:(id)sender {
     [self startAnimating:56 pictureName:@"scratch"];

}

- (IBAction)pie:(id)sender {
     [self startAnimating:24 pictureName:@"pie"];
    
}

- (IBAction)cymbal:(id)sender {
     [self startAnimating:13 pictureName:@"cymbal"];
}

- (IBAction)foot_right:(id)sender {
     [self startAnimating:30 pictureName:@"foot_right"];
}

- (IBAction)foot_left:(id)sender {
     [self startAnimating:30 pictureName:@"foot_left"];
}

- (IBAction)angry:(id)sender {
     [self startAnimating:26 pictureName:@"angry"];
    
}

- (IBAction)stomach:(id)sender {
 [self startAnimating:34 pictureName:@"stomach"];
    
}

- (IBAction)fail:(id)sender {
    
    [self startAnimating:81 pictureName:@"knockout"];
 
}

-(void)startAnimating:(int)imageCount  pictureName:(NSString*)pictureName
{
    //如果当前动画正在进行则不在进行其他动画
    if ([self.imageCatDrink isAnimating]) {
        return;
    }
  //1、动态加载图片到NSArray中
    NSMutableArray*imageArray=[NSMutableArray array];
    for (int i=0; i<imageCount; i++) {
        NSString *imageName=[NSString stringWithFormat:@"%@_%02d.jpg",pictureName,i];
        //通过ImageNamed 这种方式加载图片 加载好的图片会一直保存在内存中，不会释放 这样下次如果再使用同样的图片的时候就不需要重新加载，因为内存里面已经有了。缺点就是：如果加载大量的图片，那么图片会一直保存在内存中 导致应用程序占用内存过大（这就叫缓存）
        
      //  UIImage *image=[UIImage imageNamed:imageName];
        //解决办法 换一种加载方法 不使用缓存
        //获取图片的完整路径
        NSString *path=[[NSBundle mainBundle]pathForResource:imageName ofType:nil];
        
        UIImage*image=[UIImage imageWithContentsOfFile:path];
        [imageArray addObject:image];
        
    }
    self.imageCatDrink.animationImages=imageArray;
    self.imageCatDrink.animationDuration=self.imageCatDrink.animationImages.count*0.1;
    self.imageCatDrink.animationRepeatCount=1;
    [self.imageCatDrink startAnimating];
    //当前动画结束后释放内存 即将self.imageCatDrink 设为nil
 //   [self.imageCatDrink performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:self.imageCatDrink.animationImages.count*0.1];
    
    


}
@end
